package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import java.io.File;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityPicRepairBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.apis.ApiManager;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class PicRepairActivity extends BaseAc<ActivityPicRepairBinding> {

    public static String imgPath;
    private boolean hasOpenSpecialEffect = false;
    private Bitmap mSpecialEffectBmp;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_repair;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPicRepairBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivReplacePicture.setOnClickListener(this);
        mDataBinding.ivSavePicture.setOnClickListener(this);

        mDataBinding.ivImgCompare.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!hasOpenSpecialEffect) return true;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Glide.with(mContext).load(imgPath).into(mDataBinding.ivPicRepairImg);
                        break;
                    case MotionEvent.ACTION_UP:
                        mDataBinding.ivPicRepairImg.setImageBitmap(mSpecialEffectBmp);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivReplacePicture://切换图片
                Intent intent = new Intent(this, SelPicActivity.class);
                intent.putExtra(Content.Replace, "Replace");
                startActivity(intent);
                break;
            case R.id.ivSavePicture://保存图片
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        repair();
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.ivPicRepairImg);
                        //保存到相册
                        File file = ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        //保存到历史记录
                        String filePath = FileUtil.generateFilePath(Content.myRecord, ".png");
                        ImageUtils.save(bitmap, filePath, Bitmap.CompressFormat.PNG);
                        emitter.onNext(file.getPath());
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
//                        PicPreviewActivity.imgPath=path;
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    private void repair() {
        showDialog(getString(R.string.loading));
        ApiManager.visionAiApi().repairOldPhoto(this, UriUtil.path2Uri(mContext, imgPath), new IReqRetCallback<Bitmap>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Bitmap result) {
                dismissDialog();
                ToastUtils.showShort(msg);
                if (result != null) {
                    mDataBinding.ivPicRepairImg.setImageBitmap(result);
                    mSpecialEffectBmp = result;
                    hasOpenSpecialEffect = true;
                }
            }
        });
    }

}